package com.cloud.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.common.base.ApiResponseEnum;
import com.cloud.common.base.enums.ApiBaseEnum;
import com.cloud.common.exception.BizException;
import com.cloud.order.mapper.OrdersMapper;
import com.cloud.order.mapper.entity.Orders;
import com.cloud.order.mapper.entity.Product;
import com.cloud.order.service.IOrdersService;
import com.cloud.order.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 *  服务实现类
 *
 * @author zhangxun
 * @since 2021-01-17
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {

    @Autowired
    private IProductService productService;

    @Override
    public ApiBaseEnum createOrder(Orders order) {
        // 1、校验订单是否存在
        if (Objects.isNull(order)){
            throw new BizException(ApiResponseEnum.FAIL,"订单不存在");
        }
        // 2、校验订单商品
        Product dbProduct = productService.getById(order.getProductId());
        if (Objects.isNull(dbProduct)){
            throw new BizException(ApiResponseEnum.FAIL,"产品不存在");
        }
        return null;
        // 3、校验下单用户是否存在
        // 4、校验订单金额是否合法
        // 5、校验订单商品数量是否合法
    }
}
